package com.hiwan.unions.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.LinearInterpolator;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.hiwan.unions.R;
import com.jwenfeng.library.pulltorefresh.view.HeadView;

/**
 * Created by llg on 2017/7/11.
 */
public class HeadRefreshView extends FrameLayout implements HeadView {
    Animation operatingAnim;
    private TextView tv;
    private ImageView arrow;
    private ImageView progressBar;

    public HeadRefreshView(Context context) {
        this(context, null);
    }

    public HeadRefreshView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public HeadRefreshView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context) {
        View view = LayoutInflater.from(context).inflate(R.layout.layout_header_view, null);
        addView(view);
        tv = (TextView) view.findViewById(R.id.header_tv);
        arrow = (ImageView) view.findViewById(R.id.header_arrow);
        progressBar = (ImageView) view.findViewById(R.id.header_image);
        operatingAnim = AnimationUtils.loadAnimation(context, R.anim.anim_loading1);
        LinearInterpolator lin = new LinearInterpolator();
        operatingAnim.setInterpolator(lin);
    }

    @Override
    public void begin() {

    }

    @Override
    public void progress(float progress, float all) {
        float s = progress / all;
        if (s >= 0.9f) {
            arrow.setRotation(180);
        } else {
            arrow.setRotation(0);
        }
        if (progress >= all - 10) {
            tv.setText("松开刷新");
        } else {
            tv.setText("下拉加载");
        }
    }

    @Override
    public void finishing(float progress, float all) {

    }

    @Override
    public void loading() {
        arrow.setVisibility(GONE);
        tv.setText("正在加载...");
        progressBar.startAnimation(operatingAnim);

    }

    @Override
    public void normal() {
        arrow.setVisibility(GONE);
        tv.setText("下拉刷新");
        progressBar.clearAnimation();
    }

    @Override
    public View getView() {
        return this;
    }
}
